<?php
/**
 * 彩票服务程序，开奖数据的获取，生成分析图，推送到客户端
 * Created by PhpStorm.
 * User: zzx
 * Date: 2017/10/28
 * Time: 下午2:19
 */


include 'config.php'; // 参数配置
include 'func.php'; // 函数
$redis = redis_con();
$prev = $config['REDIS_PREFIX'] . 'datas_'; // redis前缀
$analyz = generate(); // 实例一个单双分析器
$analyz_kl8 = generate_kl8(); // 实例一个单双分析器
// 时时彩当期信息
$u1 = 'http://caipiao.163.com/order/preBet_periodInfoTime.html?gameEn=ssc&cache=1509008328582';
$u1_kl8 = 'http://chart.cp.360.cn/int/qcurissue?LotID=265108';
// 获取近期开奖信息
$u2 = 'http://caipiao.163.com/award/getAwardNumberInfo.html?gameEn=ssc&cache=1509008420470&periodNum=20';
$u2_kl8 = 'http://chart.cp.360.cn/zst/qkj/?lotId=265108';
// 查询某一期的信息
$u3 = 'http://caipiao.163.com/award/getAwardNumberInfo.html?gameEn=ssc&cache=1509009076076&period=';
$u3_kl8 = 'http://chart.cp.360.cn/zst/qkj/?lotId=265108';


init(); // 初始化

$server = new swoole_websocket_server("0.0.0.0", 9915);

$server->set(array(
    'daemonize' => 0, // 守护进程
//    'worker_num' => 2, // 工作进程数量，本地虚拟机环境使用，测试和生产环境注意修改
//    'task_worker_num' => 8, // 异步任务进程数量
    'pid_file' => __DIR__ . '/serv.php.pid', // server进程PID
));

$server->on('WorkerStart', function ($serv, $worker_id) use ($redis, $u1, $prev, $u1_kl8) {
//    echo "WorkerStart{$worker_id}\n";
    if ($worker_id == 0) {
        get_current(); // 获取当期信息
        get_current_kl8(); // 获取当期信息
    }
    
    swoole_timer_tick(5000, function () use ($serv, $worker_id, $redis, $u1, $prev, $u1_kl8) {
        if ($worker_id == 0) {
            
            //check redis the current opentime
            $get_ssc = false;
            if($redis->hExists($prev . 'ssc_wait', 'opentime')){
                $ssc_wait = $redis->hGetAll($prev . 'ssc_wait');
                $time = time();
                if($ssc_wait['opentime'] < $time) {
                    $get_ssc = true;
                }
            }else{
                $get_ssc = true;
            }
            
            if($get_ssc){
                $res = curl($u1, 5000);
                $res = json_decode($res, true);
                if($res['status'] == 0 && $res['secondsLeft'] > 0){
                    $redis->hmSet($prev . 'ssc_wait', [
                        'currentPeriod' => $res['currentPeriod'],
                        'opentime' => time() + round($res['secondsLeft'] / 1000),
                    ]);
                }
            }
            //end get ssc
            
            
            //check redis the kl8 opentime
            $get_kl8 = false;
            if($redis->hExists($prev . 'kl8_wait', 'opentime')){
                $kl8_wait = $redis->hGetAll($prev . 'kl8_wait');
                $time = time();
                if($kl8_wait['opentime'] < $time) {
                    $get_kl8 = true;
                }
            }else{
                $get_kl8 = true;
            }
            
            if($get_kl8){
                $res = curl($u1_kl8, 5000);
                $res = json_decode($res, true);
                if($res['IsOpen'] == 0 && $res['EndTime'] > 0){
                    $redis->hmSet($prev . 'kl8_wait', [
                        'currentPeriod' => $res['Issue'],
                        'opentime' => $res['EndTime'],
                    ]);
                }
            }
            //end get kl8
        }
    });
    
});

$server->on('Request', function ($request, $response) {

});

$server->on('Task', function ($server, $task_id, $from_id, $data) {

});

$server->on('open', function (swoole_websocket_server $server, $request) {

});

$server->on('Finish', function ($server, $task_id, $data) {

});

$server->on('message', function (swoole_websocket_server $server, $frame) {

});

$server->on('close', function ($ser, $fd) {

});

$server->start();
